package com.amazon.ags.client.whispersync;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.amazon.ags.api.ErrorCode;
import com.amazon.ags.api.whispersync.SynchronizeCallback;
import com.amazon.ags.client.whispersync.savedgame.JsonSummaryMarshaller;
import com.amazon.ags.client.whispersync.savedgame.PendingDownload;
import com.amazon.ags.client.whispersync.savedgame.SummaryMarshaller;
import com.amazon.ags.client.whispersync.savedgame.SummaryRepository;
import com.amazon.ags.constants.whispersync.SynchronizeResultKey;
import java.io.IOException;

public abstract class SynchronizeCallbackHandler extends Handler
{
  private static final String FEATURE_NAME = "STC";
  private static final String TAG = "STC_" + SynchronizeCallbackHandler.class.getSimpleName();
  private SynchronizeCallback callback;
  private final SummaryMarshaller marshaller = new JsonSummaryMarshaller();
  private final SummaryRepository summaryRepository;

  public SynchronizeCallbackHandler(SynchronizeCallback paramSynchronizeCallback, SummaryRepository paramSummaryRepository)
  {
    this.callback = paramSynchronizeCallback;
    this.summaryRepository = paramSummaryRepository;
  }

  private void processDownload(Bundle paramBundle)
  {
    Log.d(TAG, "Download Success");
    byte[] arrayOfByte = paramBundle.getByteArray("STC_DATA");
    if ((arrayOfByte == null) || (arrayOfByte.length == 0))
    {
      Log.e(TAG, "Expected downloaded data");
      this.callback.onSynchronizeFailure(ErrorCode.IO_ERROR);
    }
    GameSummary localGameSummary;
    do
    {
      return;
      Log.d(TAG, "Received callback DOWNLOAD_SUCCESS data.length = " + arrayOfByte.length);
      String str = paramBundle.getString("CLOUD_GAME_SUMMARY");
      localGameSummary = this.marshaller.unmarshal(str);
      if (!storePendingDownload(arrayOfByte, localGameSummary))
      {
        this.callback.onSynchronizeFailure(ErrorCode.IO_ERROR);
        return;
      }
    }
    while (!processDownloadedData(arrayOfByte));
    this.summaryRepository.storeSummary(localGameSummary);
    this.summaryRepository.removePendingUpload();
    this.summaryRepository.removePendingDownload();
  }

  private void processUpload(Bundle paramBundle)
  {
    Log.d(TAG, "Upload Success");
    String str = paramBundle.getString("CLOUD_GAME_SUMMARY");
    GameSummary localGameSummary = null;
    if (str != null)
      localGameSummary = this.marshaller.unmarshal(str);
    this.summaryRepository.storeSummary(localGameSummary);
    this.summaryRepository.removePendingUpload();
    this.callback.onGameUploadSuccess();
  }

  public final void handleMessage(Message paramMessage)
  {
    Log.i(TAG, "Handling Synchronize response message");
    Bundle localBundle = paramMessage.getData();
    if (localBundle == null)
      this.callback.onSynchronizeFailure(ErrorCode.UNRECOVERABLE);
    String str = localBundle.getString("SYNCHRONIZE_RESULT");
    if (str == null)
    {
      this.callback.onSynchronizeFailure(ErrorCode.UNRECOVERABLE);
      Log.e(TAG, "Received null synchronize result");
      return;
    }
    try
    {
      SynchronizeResultKey localSynchronizeResultKey = SynchronizeResultKey.valueOf(str);
      switch (1.$SwitchMap$com$amazon$ags$constants$whispersync$SynchronizeResultKey[localSynchronizeResultKey.ordinal()])
      {
      default:
        Log.w(TAG, "Unexpected message received.  Result=" + localSynchronizeResultKey + " " + paramMessage.toString());
        return;
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      }
    }
    catch (IllegalArgumentException localIllegalArgumentException)
    {
      this.callback.onSynchronizeFailure(ErrorCode.UNRECOVERABLE);
      Log.e(TAG, "Received unrecognized synchronize result of: <" + str + ">");
      return;
    }
    Log.d(TAG, "Already Synchronized");
    this.callback.onAlreadySynchronized();
    return;
    Log.d(TAG, "Conflict Deferred");
    this.callback.onConflictDeferral();
    return;
    processUpload(localBundle);
    return;
    processDownload(localBundle);
    return;
    Log.e(TAG, "Synchronize Failed");
    int i = localBundle.getInt("ERROR");
    this.callback.onSynchronizeFailure(ErrorCode.fromServiceResponseCode(i));
  }

  protected abstract boolean processDownloadedData(byte[] paramArrayOfByte);

  protected final boolean storePendingDownload(byte[] paramArrayOfByte, GameSummary paramGameSummary)
  {
    PendingDownload localPendingDownload = new PendingDownload(paramArrayOfByte, paramGameSummary);
    try
    {
      this.summaryRepository.storePendingDownload(localPendingDownload);
      return true;
    }
    catch (IOException localIOException)
    {
      Log.e(TAG, "Failed storing PendingDownload");
    }
    return false;
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.ags.client.whispersync.SynchronizeCallbackHandler
 * JD-Core Version:    0.6.2
 */